/*----------------------------------------------------------------------
  - File name      : STC8Ax_SYSCLK.c
  - Author         : Quark Team
  - Update date    : 2020-12-13                  
  -	Copyright      : Gevico Electronic studio   
  - Module comments: System clock mdule driver.
-----------------------------------------------------------------------*/
/*-----------------------------------------------------------------------
|                               INCLUDES                                |
-----------------------------------------------------------------------*/
#include "STC8Ax_SYSCLK.h"
/*-----------------------------------------------------------------------
|                                 DATA                                  |
-----------------------------------------------------------------------*/
uint32 SYSCLK_FRE = 0;
/*-----------------------------------------------------------------------
|                               FUNCTION                                |
-----------------------------------------------------------------------*/

/*--------------------------------------------------------
| @Description: System clock init function               |
| @param      : const SYSCLK_InitType *SYSCLKn           |
| @return     : FSCSTATE : Success / Fail                |
--------------------------------------------------------*/

FSCSTATE SYSCLK_Init(const SYSCLK_InitType *SYSCLKn)
{
		EAXFR_ENABLE(); /* Enable access to the internal extended RAM area */

		if(SYSCLKn -> MCLKSrc != AUTO)
		{
			
			switch(SYSCLKn -> MCLKSrc) /* Select master clock source */
			{
				case HIRC: SYSCLK.CKSEL_REG = 0x00; SYSCLK.IRC24MCR_REG |= 0x80; break;
				case LIRC: SYSCLK.CKSEL_REG = 0x01; SYSCLK.IRC32KCR_REG |= 0x80; break;
				case XOSC: SYSCLK.CKSEL_REG = 0x03; SYSCLK.XOSCCR_REG |= 0xC0;   break;
			}
			
			/* Wait for the clock source switch to complete */
			while(!( (SYSCLK.IRC24MCR_REG & 0x01)   || 
							 (SYSCLK.IRC32KCR_REG & 0x01)   || 
							 (SYSCLK.XOSCCR_REG   & 0x01)  ));
			
      IRTRIM = SYSCLKn -> IRCTRIM;
			LIRTRIM = SYSCLKn -> LIRCTRIM;
			
			if(SYSCLKn -> MCLKDiv == 0) SYSCLKn -> MCLKDiv += 1;
			SYSCLK.CLKDIV_REG = SYSCLKn -> MCLKDiv;  /* Master clock frequency division */
			
	  }
		
		SYSCLK.CKSEL_REG |= SYSCLKn -> SCLKDiv << 4;   /* System clock frequency division */
		SYSCLK.CKSEL_REG |= SYSCLKn -> SCLKOutPin << 3;
		EAXFR_DISABLE(); /* Disable access to the internal extended RAM area */
		
		/* Get system clock frequency */
		SYSCLK_FRE = GET_SYSCLIK_FRE();
	
		return FSC_SUCCESS; /* Function self checking succeeded */
}

/*--------------------------------------------------------
| @Description: Get System clock frequendy function      |
| @param      : SYSCLK_InitType *SYSCLKn                 |
| @return     : FSCSTATE : Success / erro                |
--------------------------------------------------------*/

uint32 GET_SYSCLIK_FRE(void)
{
	uint32 Count;
	
	/* Frequency deviation, calculate the approximate data */
	Count = 24000000UL + ((int32)((int32)IRTRIM - (int32)IRC_24M) * 0xD2F0UL);
	Count /= SYSCLK.CLKDIV_REG;  /*Divide by division value */

	return Count;
}

/*-----------------------------------------------------------------------
|          END OF FLIE        (C) COPYRIGHT Gevico Electronics          | 
-----------------------------------------------------------------------*/

